﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference name="Telerik.Web.UI.Common.Core-vsdoc.js" assembly="Telerik.Web.UI" />
/// <reference path="~/Clients/Scripts/jQuery/jquery-1.6.1-vsdoc.js"/>
/// <reference path="~/Clients/Scripts/jQuery/jquery-ui-1.8.13.custom.min.js"/>
/// <reference path="~/Clients/Scripts/jQuery/jquery.ajaxmanager-3.12.debug.js"/>
/// <reference path="~/Clients/Scripts/jQuery/jquery.json.debug.js"/>
/// <reference path="~/Clients/Scripts/jQuery/jquery.component.debug.js"/>
/// <reference path="~/Clients/Scripts/globals.debug.js"/>
/// <reference path="~/Clients/Scripts/utils.debug.js"/>
/// <reference path="~/Clients/Scripts/errorHandling.debug.js"/>
/// <reference path="~/Clients/Scripts/loadingPanels.debug.js"/>
/// <reference path="~/Clients/Scripts/common.debug.js"/>
/// <reference path="~/Clients/Scripts/jqueryLookup.debug.js"/>
/// <reference path="~/Clients/Scripts/url.debug.js"/>
/// <reference path="~/Clients/Scripts/queryObject.debug.js"/>

Type.registerNamespace("IRC.Pages.Cases");

IRC.Pages.Cases.CaseSearch = function (element) {
    IRC.Pages.Cases.CaseSearch.initializeBase(this, [element]);
    this._mainOwner;
    this._controlsHolder;

    this._searchAM;


    this._onInitDelegate;
    this._onLoadDelegate;
    this._searchCriteria;

    this._searched = false;
    this._lastSelectedId;
    this._detailId;
    
}

IRC.Pages.Cases.CaseSearch.prototype = {
    _controls: function () {
        if (!this._controlsHolder) {
            this._controlsHolder = new (function (element) {
                this.jq = new GreeNova.JqueryLookup(element);
                this.RadAjaxLoadingPanel = new GreeNova.JqueryLookup(this.jq, "[id$='RadAjaxLoadingPanel']", true);
                this.RadGridSearchResults = new GreeNova.JqueryLookup(this.jq, "[id$='RadGridSearchResults']", true);

                this.ButtonSearch = new GreeNova.JqueryLookup(this.jq, "[id$='ButtonSearch']");
                this.ButtonNewSearch = new GreeNova.JqueryLookup(this.jq, "[id$='ButtonNewSearch']");


                this.RadTabStripSearchConditions = new GreeNova.JqueryLookup(this.jq, "[id$='RadTabStripSearchConditions']", true);
                this.RadMultiPageSearchConditions = new GreeNova.JqueryLookup(this.jq, "[id$='RadMultiPageSearchConditions']", true);
                this.FTransaction = new GreeNova.JqueryLookup(this.RadMultiPageSearchConditions, "[id$='RadPageViewSearchConditionsCases']");

                this.F = {
                    TextBoxCaseNumber: new GreeNova.JqueryLookup(this.FTransaction, "[id$='TextBoxCaseNumber']"),
                    TextBoxSerialNumber: new GreeNova.JqueryLookup(this.FTransaction, "[id$='TextBoxSerialNumber']"),
                    RadComboBoxStatus: new GreeNova.JqueryLookup(this.FTransaction, "[id$='RadComboBoxStatus']", true),
                    RadComboBoxCustomer: new GreeNova.JqueryLookup(this.FTransaction, "[id$='RadComboBoxCustomer']", true),
                    RadComboBoxModel: new GreeNova.JqueryLookup(this.FTransaction, "[id$='RadComboBoxModel']", true),
                    RadDatePickerBIFromDate: new GreeNova.JqueryLookup(this.FTransaction, "[id$='RadDatePickerBIFromDate']", true),
                    RadDatePickerBIToDate: new GreeNova.JqueryLookup(this.FTransaction, "[id$='RadDatePickerBIToDate']", true),
                    RadDatePickerBOFromDate: new GreeNova.JqueryLookup(this.FTransaction, "[id$='RadDatePickerBOFromDate']", true),
                    RadDatePickerBOToDate: new GreeNova.JqueryLookup(this.FTransaction, "[id$='RadDatePickerBOToDate']", true)
                };


                this.PanelResult = new GreeNova.JqueryLookup(this.jq, "[id$='PanelResult']");

                this.RadGridSearchResults = new GreeNova.JqueryLookup(this.jq, "[id$='RadGridSearchResults']", true);
                this.RadToolTipManagerLog = new GreeNova.JqueryLookup(this.jq, "[id$='RadToolTipManagerLog']", true);
            })(this.get_element());
        }
        return this._controlsHolder;
    },

    initialize: function () {
        IRC.Pages.Cases.CaseSearch.callBaseMethod(this, 'initialize');

        Sys.Application.add_init(this._onInitDelegate = this._onInit.setCtx(this));
        Sys.Application.add_load(this._onLoadDelegate = this._onLoad.setCtx(this));
    },
    dispose: function () {
        Sys.Application.remove_load(this._onLoadDelegate);
        Sys.Application.remove_init(this._onInitDelegate);

        if (this._controlsHolder) {
            this._controlsHolder.ImageButtonCaseSearchSave.jq().unbind();
            this._controlsHolder.ImageButtonRemoveCustomer.jq().unbind();

        }
        this._controlsHolder = null;

        IRC.Pages.Cases.CaseSearch.callBaseMethod(this, 'dispose');
    },

    get_mainOwner: function () {
        return this._mainOwner;
    },
    set_mainOwner: function (value) {
        this._mainOwner = value;
    },

    _onInit: function () {
        var controls = this._controls();
        controls.ButtonSearch.jq().click(this._search.setCtx(this, { epd: true, fargs: true }));
        controls.ButtonNewSearch.jq().click(this._newSearch.setCtx(this, { epd: true }));
    },

    _onLoad: function () {
        var controls = this._controls();
        var rg = controls.RadGridSearchResults.component();
        rg.add_command(this._radGridSearchResults_command.setCtx(this));
        rg.add_rowDataBound(this._radGridSearchResults_rowDataBound.setCtx(this));
        rg.add_rowSelected(this._radGridSearchResults_rowSelected.setCtx(this));
    },

    _radGridSearchResults_SelectSelected: function () {
        var controls = this._controls();
        var gMT = controls.RadGridSearchResults.component().get_masterTableView();
        gMT.clearSelectedItems();
        if (!this._lastSelectedId)
            return;
        var items = gMT.get_dataItems();
        for (var i in items) {
            var item = items[i].get_dataItem();
            if (item !== null) {
                var iid = item.RowGuid;
                if (iid === this._lastSelectedId) {
                    gMT.selectItem(i);
                    return;
                }
            }
        }
    },
    _radGridSearchResults_command: function (sender, args) {
        args.set_cancel(true);

        if (!this._loading)
            this._search()
    },

    _radGridSearchResults_rowSelected: function (sender, args) {
        var item = args.get_item();
        var dataItem = item.get_dataItem();
        var id = dataItem.RowGuid;

        this._showTransactionDetail(id);
        this._lastSelectedId = id;
    },

    _radGridSearchResults_rowDataBound: function (sender, args) {
        var item = args.get_item();
        var dataItem = args.get_dataItem();
    },


    get_searchAM: function () {
        if (!this._searchAM)
            this._searchAM = GreeNova.Utils.AjaxManager.createForWS('_searchAM');
        return this._searchAM;
    },

    _RadGridSearchResults_command: function (sender, args) {
        args.set_cancel(true);
        this.updateHistory(true)
    },

    _getSearchCriteria: function () {
        var controls = this._controls();
        var crit = {
            CaseNumber: controls.F.TextBoxCaseNumber.jq().val(),
            SerialNumber: controls.F.TextBoxSerialNumber.jq().val(),
            Status: controls.F.RadComboBoxStatus.component().get_value(),
            Customer: controls.F.RadComboBoxCustomer.component().get_value(),
            Model: controls.F.RadComboBoxModel.component().get_value(),
            RadDatePickerBIFromDate: GreeNova.Utils.RadDatePicker.toServerDateStr(controls.F.RadDatePickerBIFromDate),
            RadDatePickerBIToDate: GreeNova.Utils.RadDatePicker.toServerDateStr(controls.F.RadDatePickerBIToDate),
            RadDatePickerBOFromDate: GreeNova.Utils.RadDatePicker.toServerDateStr(controls.F.RadDatePickerBOFromDate),
            RadDatePickerBOToDate: GreeNova.Utils.RadDatePicker.toServerDateStr(controls.F.RadDatePickerBOToDate)
        };

        return crit;
    },

    _newSearch: function () {
        var am = this.get_searchAM();
        am.clear(true);
        am = this.get_detailAM();
        am.clear(true);

        var controls = this._controls();
        controls.RadTabStripSearchConditions.component().findTabByValue('Transaction').select();



        GreeNova.Utils.RadGrid.bindEmpty(controls.RadGridSearchResults, GreeNova.GridPagerVisibility.Visible, GreeNova.GridPagerVisibility.Hidden, true);
        controls.DetailView.jq().hide();
        this._detailId = undefined;
        this._lastSelectedId = undefined;
        this._searched = false;
    },
    _search: function (newSearch) {
        ;
        var controls = this._controls();
        GreeNova.Utils.RadGrid.showStatusAjaxRequest(controls.RadGridSearchResults);
        if (newSearch) {
            this._searchCriteria = this._getSearchCriteria();
        }
        var grid = GreeNova.Utils.RadGrid.getBindingInfo(controls.RadGridSearchResults, newSearch);

        var am = this.get_searchAM();
        am.clear(true);
        am.add({
            url: GreeNova.Globals.resolveUrl('ws/Cases.asmx/Cases_Search'),
            data: $.json.serialize({
                criteria: this._searchCriteria,
                grid: grid
            }),
            success: function (data, status, xhr) {
                debugger;
                this._loading = true;
                var result = data.d;
                this._searched = true;
                //if (newSearch)
                //    controls.RadGridSearchResults.component().get_masterTableView().set_currentPageIndex(0);
                if (result && result.Success)
                    GreeNova.Utils.RadGrid.bind(controls.RadGridSearchResults, result.Data.Count, result.Data.Items, newSearch);
                else
                    GreeNova.Utils.RadGrid.bind(controls.RadGridSearchResults);
                this._radGridSearchResults_SelectSelected();
                this._loading = false;
            } .setCtx(this),
            error: function (xhr, status, errorThrown, opt) {
                GreeNova.ErrorHandling.jqAMError(xhr, opt);
            } .setCtx(this),
            complete: function (xhr, status) {
                GreeNova.Utils.RadGrid.hideStatusAjaxRequest(controls.RadGridSearchResults);
            } .setCtx(this)
        });
    }
}
IRC.Pages.Cases.CaseSearch.registerClass('IRC.Pages.Cases.CaseSearch', Sys.UI.Control);
